//
//  XYDownloadHeadView.swift
//  XYReadBook
//
//  Created by fox on 2017/10/24.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit


/// 组头View
class XYDownloadHeadView: UITableViewHeaderFooterView {
    var isEndDownloadlab:Bool = false {
        didSet {
            endDownloadLab.isHidden = false
            selectedBtn.isHidden = true
        }
    }
    var chapters:[Chapters] = [] {
        didSet {
            // 判断是否全部已经下载成功了
            var falg:Bool = true

            for index in 0...chapters.count - 1 {
                let curentLink = chapters[index].link.urlEncode!
                let urlKey = BaseUrl.chapter.rawValue + curentLink
                let iscontains = HTTPCache.xwj_containsObject(forKey: urlKey)
                if iscontains == false {
                    falg = false
                }
            }

            if falg == true {
                // 显示已经下载
                endDownloadLab.isHidden = false
                selectedBtn.isHidden = true
            }
        }
    }

    var isSelected:Bool = false {
        didSet {
            if isSelected == true {
                selectedBtn.isSelected = true
            }else {
                 selectedBtn.isSelected = false
            }
        }
    }


    var isUpImageView:Bool = false {
        didSet {
            if isUpImageView == true {
                UIView.animate(withDuration: 0.4) {
                    let num = ((2 * M_PI) / 4) * Double(1)
                    let transform = CGAffineTransform.init(rotationAngle: CGFloat(num))
                    self.upImageView.transform = transform
                    self.upImageView.tag = 1
                }
                self.layoutIfNeeded()
            }
        }
    }
    var chapterText:String = "" {
        didSet{
            chapterLab.text = chapterText
        }
    }
    var selected:((_ selectedBtn:UIButton) ->Void)?
    var selectedChapter:((_ chapter:UIButton) ->Void)?
    let chapterLab = UILabel()
    let selectedBtn = UIButton()
    let upImageView = UIImageView()
    let lineView = UIView()
    let selectedChapterBtn = UIButton()
    let endDownloadLab = UILabel()


    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        setupUI()
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        selectedChapterBtn.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth / 2, height: 40)
        upImageView.frame = CGRect.init(x: 12, y: 10, width: 20, height: 20)
        chapterLab.frame = CGRect.init(x: upImageView.frame.maxX + 6, y: 12, width: ScreenWidth - 40, height: 24)
        selectedBtn.frame = CGRect.init(x: ScreenWidth - 54, y: 12, width: 40, height: 20)
        lineView.frame = CGRect.init(x: 0, y: self.height - 1, width: self.width, height: 1)
        endDownloadLab.frame =  CGRect.init(x: ScreenWidth - 74, y: 12, width: 60, height: 24)
    }

    func setupUI() {
        addSubview(upImageView)
        addSubview(chapterLab)
        addSubview(selectedBtn)
        addSubview(selectedChapterBtn)
        addSubview(lineView)
        selectedChapterBtn.addTarget(self, action: #selector(clickChapter(sender:)), for: .touchUpInside)
//        selectedChapterBtn.backgroundColor = UIColor.clear
        chapterLab.text = "第一章 - 第五十章"
        upImageView.image = UIImage.init(named: "but_xiala_01")
        selectedBtn.setImage(UIImage.init(named: "payPick"), for: .normal)
        selectedBtn.setImage(UIImage.init(named: "payPick_selected"), for: .selected)
        selectedBtn.addTarget(self, action: #selector(clickSelected), for: .touchUpInside)
         lineView.backgroundColor = UIColor.init(red: 243/255.0, green: 243/255.0, blue: 243/255.0, alpha: 1)

        addSubview(endDownloadLab)
        endDownloadLab.text = "已下载"
        endDownloadLab.textColor = UIColor.gray
        endDownloadLab.isHidden = true

    }

    func clickChapter(sender:UIButton) {
          selectedChapter?(sender)

    }


    /// 点击选择
    func clickSelected() {
        selectedBtn.isSelected = !selectedBtn.isSelected
        selected?(selectedBtn)
    }
}
